//
//  AliPayManager.m
//  TomatoDemo
//
//  Created by 冯洪建 on 15/8/18.
//  Copyright (c) 2015年 hongjian feng. All rights reserved.
//

#import "AliPayManager.h"
#import <AlipaySDK/AlipaySDK.h>
#import "AppDelegate.h"

@implementation AliPayManager

+ (instancetype)sharedAliPayManager
{
    static AliPayManager *singleTon = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singleTon = [[AliPayManager alloc] init];
    });
    return singleTon;
}

- (BOOL)aLiPayWithApplication:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    if(!url)
    {
        return NO;
    }
    //    如果极简 SDK 不可用,会跳转支付宝钱包进行支付,需要将支付宝钱包的支付结果回传给 SDK
    if ([url.host isEqualToString:@"safepay"])
    {
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            [[NSNotificationCenter defaultCenter]postNotificationName:@"alipayResult" object:[resultDic objectForKey:@"resultStatus"]];
        }];
    }
    if ([url.host isEqualToString:@"platformapi"])
    {
        //支付宝钱包快登授权返回 authCode
        [[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
        }];
    }
    return YES;
}


- (void)payWithDic:(NSDictionary *)dic
{
    NSString *signedString = dic[@"sign"];
    NSString *orderSpec = dic[@"data"][@"payFormOrData"];
    
    if (!orderSpec || orderSpec.length < 1 || !signedString)
    {
        [self presentMessageTips:NSLocalizedString(@"Missing Item Parameter", nil)];
        return;
    }
    if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"alipay://"]])
    {
        [[[UIApplication sharedApplication] windows] objectAtIndex:0].hidden = NO;
    }

    if ((signedString != nil)&&(orderSpec != nil))
    {
        [[AlipaySDK defaultService] payOrder:orderSpec fromScheme:schemeUrl callback:^(NSDictionary *resultDic) {
            NSLog(@"%@", resultDic);
            NSString * result = [resultDic objectForKey:@"resultStatus"];
            
            //当手机没有没有安装支付宝客户端时，调用支付宝web网页 回调隐藏底层 window
            //            if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"alipay://"]]) {
            //                [[[UIApplication sharedApplication] windows] objectAtIndex:0].hidden = YES;
            //             }
            
//            NSString *strMsg,*strTitle = [NSString stringWithFormat:@"支付结果"];
            NSString *strMsg,*strTitle = [NSString stringWithFormat:@"%@",NSLocalizedString(@"Payment Result", nil)];

            if ([result isEqualToString:@"9000"])
            {
                strMsg=NSLocalizedString(@"Payment Result: Successful!", nil);
                NSLog(@"支付宝————支付结果result = %@  ---    支付成功", resultDic);
                [[NSNotificationCenter defaultCenter]postNotificationName:@"alipayResult" object:[resultDic objectForKey:@"resultStatus"]];
            }
            else
            {
                strMsg = [NSString stringWithFormat:@"%@",NSLocalizedString(@"Payment Result: Failed!", nil)];
                NSLog(@"支付宝————支付结果result = %@   ------  支付失败", resultDic);
                [[NSNotificationCenter defaultCenter]postNotificationName:@"alipayResult" object:[resultDic objectForKey:@"resultStatus"]];
            }
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }];
    }
}
@end
